<template>
  <el-table
    :data="tableData.filter(data => !search || data.fname.toLowerCase().includes(search.toLowerCase()))"
    style="width: 100%"
    height=780
    border>
    <el-table-column
      label="菜品ID"
      prop="fid">
    </el-table-column>
    <el-table-column
      label="菜品名称"
      prop="fname">
    </el-table-column>
    <el-table-column
      label="菜品描述"
      prop="fdesc">
    </el-table-column>
    <el-table-column
      label="价格"
      prop="price">
    </el-table-column>
    <el-table-column prop="image" label="图片" width="120">
      <template slot-scope="scope">
        <el-image
          style="width: 100px; height: 100px"
          :src="scope.row.image"
          fit="fill"></el-image>
      </template>
    </el-table-column>
    <el-table-column
      align="right">
      <template slot="header" slot-scope="scope">
        <el-input
          v-model="search"
          size="mini"
          placeholder="输入关键字搜索"/>
      </template>
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.row.fid)">修改</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete(scope.row.fid)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
  import axios from "axios";
  export default {
    data() {
      return {
        tableData:[
          {
            fid:'',
            fname: '',
            fdesc: '',
            price: '',
            image: '',
          },
        ],
        search:'',
      }
    },
    mounted() {
      this.findAllFoods();
    },

    methods: {
      // handleEdit(index, row) {
      //   console.log(index, row);
      // },
      handleEdit:function(id){
        this.$router.push("/foodUpdate/"+id)
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
      findAllFoods(){
        axios.get("/api/selectAllFood").then(res=>{
          this.tableData=res.data;
        })
      },
      handleDelete: function (fid) {

        if(confirm("你确认删除该条目吗？？？") == true){

          axios.post("/api/deleteFoodById/" + fid).then(res => {

            if(res.data == "1"){
              this.$router.go(0)
            }else{
              alert("删除失败！！！");
            }
          });
        }else{
          this.$router.push("/foodsList/")
        }
      }


    },
  }
</script>


